package itxx.itxx;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/LoginServletDemo")
public class LoginServletDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //设置post请求的字符集
        request.setCharacterEncoding("utf-8");
        //获取请求数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //判断用户名和密码是否正确
        if("admin".equals(username) && "123".equals(password)){
            //正确跳转到首页
            request.getRequestDispatcher("/demo1/index.html").forward(request,response);
        }else{
            //密码或者用户名错误
            request.getRequestDispatcher("/demo2/login.html").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
